package monsmon.game.parse;

import monsmon.game.*;
import monsmon.game.tmlt.*;
import java.util.*;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.io.*;
import javax.xml.namespace.QName;
import monsmon.util.*;
import java.awt.Color;

class XMLParser{

	private static XMLInputFactory 
		__xmlfin = XMLInputFactory.newInstance();

	static XMLEventReader __get_reader_xml(String s){
		try{
			return __xmlfin.createXMLEventReader(
					new StringReader(s));
		}catch(XMLStreamException e){
			//TODO:log error
		}
		return null;
	}

	static XMLEvent __get_event(XMLEventReader r){
		try{
			return r.nextEvent();
		}catch(Exception ex){return null;}
	}

	static XMLEvent __peek_event(XMLEventReader r){
		try{
			return r.peek();
		}catch(XMLStreamException ex) { return null; }
	}

	static String __get_str(XMLEvent e){
		if(e.isStartElement()){
			return e.asStartElement().getName().getLocalPart();
		}else if(e.isEndElement()){
			return e.asEndElement().getName().getLocalPart();
		}
		return "";
	}

	static String __get_val(StartElement e, String name){
		Attribute atrb = e.getAttributeByName(new QName(name));
		if(atrb != null){
			return atrb.getValue();
		}
		return "";
	}

	static String __get_valn(StartElement e, String name){
		Attribute atrb = e.getAttributeByName(new QName(name));
		if(atrb != null){
			return atrb.getValue();
		}
		return null;
	}

        private static void __set_generic_val(XMLEventReader r,
                        StartElement e,
                        Map<String, String> paras){
                String nm = __get_str(e);
                String val = __get_valn(e, "val");
                if(val != null){
                        paras.put(nm, val);
                }else{
                        XMLEvent nx = __peek_event(r);
                        if(nx != null && nx.isCharacters()){
                                String ch = nx.asCharacters().getData();
                                paras.put(nm, ch);
                        }
                }
        }



	static EntT __p_ent(XMLEventReader rdr, StartElement se){
		HashMap<String, String> tparas = new HashMap<String, String>();
		tparas.put("name", __get_valn(se, "name"));
		while(rdr.hasNext()){
			XMLEvent e = __get_event(rdr);
			if(e == null) break;
			if(e.isStartElement()){
				String n = __get_str(e);
				se = e.asStartElement();
				if("color".equals(n)){
					String fgs = __get_valn(se, "fg");
					String bgs = __get_valn(se, "bg");
					tparas.put("fg", fgs);
					tparas.put("bg", bgs);
				}else{
					__set_generic_val(rdr, se, tparas);
				}
			}
			if(e.isEndElement()){
				if("entity".equals(__get_str(e))){
					break;
				}
			}
		}
		Params p = new Params(tparas);
		return new EntT(p);
	}

	static TileT __p_tile(XMLEventReader rdr, StartElement se){
		HashMap<String, String> tparas = new HashMap<String, String>();
		tparas.put("name", __get_valn(se, "name"));
		while(rdr.hasNext()){
			XMLEvent e = __get_event(rdr);
			if(e == null) break;
			if(e.isStartElement()){
				String n = __get_str(e);
				se = e.asStartElement();
				if("color".equals(n)){
					String fgs = __get_valn(se, "fg");
					String bgs = __get_valn(se, "bg");
					tparas.put("fg", fgs);
					tparas.put("bg", bgs);
				}else{
					__set_generic_val(rdr, se, tparas);
				}
			}
			if(e.isEndElement()){
				if("tile".equals(__get_str(e))){
					break;
				}
			}
		}
		Params p = new Params(tparas);
		return new TileT(p);
	}



	static MoveT __read_move(XMLEventReader rdr, StartElement st){
		Map<String, String> mparas = new HashMap<>();
		mparas.put("name", __get_valn(st, "name"));
		while(rdr.hasNext()){
			XMLEvent e = __get_event(rdr);
			if(e.isEndElement()){
				if(__get_str(e).equals("move")){
					break;
				}
			}
			if(e.isStartElement()){
				String nm = __get_str(e);
				StartElement se = e.asStartElement();
				__set_generic_val(rdr, se, mparas);
			}
		}
		Params p = new Params(mparas);
		return new MoveT(p);
	}

	static MonsT __read_mons(XMLEventReader r, StartElement st){
		String name = __get_val(st, "name");
		String fname = __get_val(st, "fname");
		int sattk = 0, acc = 0, attk = 0;
		while(r.hasNext()){
			XMLEvent e = __get_event(r);
			if(e == null) break;
			if(e.isEndElement()){
				if(__get_str(e).equals("move")){
					break;
				}
			}
			if(e.isStartElement()){
				String nm = __get_str(e);
				StartElement se = e.asStartElement();
				if(nm.equals("acc")){
					acc = Integer.
						parseInt(__get_val(se, "val"));
				}else if(nm.equals("sattk")){
					sattk = Integer.
						parseInt(__get_val(se, "val"));
				}else if(nm.equals("attk")){
					attk = Integer.
						parseInt(__get_val(se, "val"));
				}
			}
		}
		if(!"".equals(name)){
			return new MonsT(name, fname);
		}
		return null;
	}


}
